package com.five.calendar.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.five.calendar.ApplicationHelper;
import com.five.calendar.common.database.DatabaseUtils;
import com.five.calendar.database.bean.ScheduleBean;
import com.five.calendar.database.bean.TargetBean;
import com.five.calendar.database.bean.TargetDayBean;
import com.five.calendar.database.table.ScheduleTable;
import com.five.calendar.database.table.TargetDayTable;
import com.five.calendar.database.table.TargetTable;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库操作类
 * Created by xin on 2017/8/29.
 */

public class DataProvider {
    private DataHelper mDataHelper;
    private static DataProvider sInstance;

    private DataProvider() {
        mDataHelper = new DataHelper(ApplicationHelper.getContext());
    }

    public static DataProvider getInstance() {
        if (null == sInstance) {
            sInstance = new DataProvider();
        }
        return sInstance;
    }

    public List<ScheduleBean> query() {
        synchronized (mDataHelper) {
            Cursor cursor = null;
            try {
                List<ScheduleBean> list = new ArrayList<>();
                cursor = mDataHelper.rawQuery("select * from " + ScheduleTable.TABLE_NAME + " order by " + ScheduleTable.STARTTIME, null);
                while (cursor.moveToNext()) {
                    ScheduleBean scheduleBean = new ScheduleBean();
                    scheduleBean.readObject(cursor, ScheduleTable.TABLE_NAME);
                    list.add(scheduleBean);
                }
                return list.isEmpty() ? null : list;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        }
        return null;
    }

    public List<ScheduleBean> queryByTime(long begin, long end){
        synchronized (mDataHelper) {
            Cursor cursor = null;
            try {
                List<ScheduleBean> list = new ArrayList<>();
                cursor = mDataHelper.rawQuery("select * from " + ScheduleTable.TABLE_NAME +" where "+ ScheduleTable.STARTTIME + " > " + begin + " and " + ScheduleTable.STARTTIME + " < " + end + " order by " + ScheduleTable.STARTTIME, null);
                Log.e("ZXX", "sql: " + "select * from " + ScheduleTable.TABLE_NAME +" where "+ ScheduleTable.STARTTIME + " > " + begin + " and " + ScheduleTable.STARTTIME + " < " + end + " order by " + ScheduleTable.STARTTIME);
                while (cursor.moveToNext()) {
                    ScheduleBean scheduleBean = new ScheduleBean();
                    scheduleBean.readObject(cursor, ScheduleTable.TABLE_NAME);
                    list.add(scheduleBean);
                }
                return list.isEmpty() ? null : list;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        }
        return null;
    }

    public ScheduleBean insert(String title, String address, long startTime, long endTime, int importance, String note, int isRemind, long remindTime) {
        synchronized (mDataHelper) {
            try {
                ScheduleBean scheduleBean = new ScheduleBean(title, address, startTime, endTime, importance, note, isRemind, remindTime);
                ContentValues values = new ContentValues();
                scheduleBean.writeObject(values, ScheduleTable.TABLE_NAME);
                scheduleBean.id = mDataHelper.getWritableDatabase().insert(ScheduleTable.TABLE_NAME, null, values);
                if (scheduleBean.id == -1) {
                    return null;
                }
                return scheduleBean;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public boolean delete(List<ScheduleBean> list) {
        synchronized (mDataHelper) {
            try {
                String[] arrays = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    arrays[i] = "delete from " + ScheduleTable.TABLE_NAME + " where " + ScheduleTable.ID + "=" + list.get(i).id;
                }
                return mDataHelper.execSQL(arrays);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean update(ScheduleBean scheduleBean) {
        synchronized (mDataHelper) {
            try {
                ContentValues values = new ContentValues();
                scheduleBean.writeObject(values, ScheduleTable.TABLE_NAME);
                return mDataHelper.execSQL(new DatabaseUtils.UpdateParams(values, ScheduleTable.ID + "=" + scheduleBean.id, ScheduleTable.TABLE_NAME));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 目标部分数据库操作
     * @return
     */
    public List<TargetBean> queryTargetBean() {
        synchronized (mDataHelper) {
            Cursor cursor = null;
            try {
                List<TargetBean> list = new ArrayList<>();
                cursor = mDataHelper.rawQuery("select * from " + TargetTable.TABLE_NAME + " order by " + TargetTable.CREATETIME, null);
                while (cursor.moveToNext()) {
                    TargetBean targetBean = new TargetBean();
                    targetBean.readObject(cursor, TargetTable.TABLE_NAME);
                    list.add(targetBean);
                }
                return list.isEmpty() ? null : list;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        }
        return null;
    }

    public List<TargetBean> queryTargetBeanForTime(long end) {
        synchronized (mDataHelper) {
            Cursor cursor = null;
            try {
                List<TargetBean> list = new ArrayList<>();
                cursor = mDataHelper.rawQuery("select * from " + TargetTable.TABLE_NAME + " where " + TargetTable.STARTTIME + "<" + end +" and " + TargetTable.ENDTIME + ">" + end + " and " + TargetTable.ISCOMPLETE + " = " + 0 + " order by " + TargetTable.CREATETIME, null);
                while (cursor.moveToNext()) {
                    TargetBean targetBean = new TargetBean();
                    targetBean.readObject(cursor, TargetTable.TABLE_NAME);
                    list.add(targetBean);
                }
                return list.isEmpty() ? null : list;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        }
        return null;
    }

    public TargetBean insertTargetBean(String title, long startTime, long endTime, String detail, String type, int color, String reminderText, int isComplete) {
        synchronized (mDataHelper) {
            try {
                TargetBean targetBean = new TargetBean(title, startTime, endTime, detail, type, color, reminderText, isComplete);
                ContentValues values = new ContentValues();
                targetBean.writeObject(values, TargetTable.TABLE_NAME);
                targetBean.id = mDataHelper.getWritableDatabase().insert(TargetTable.TABLE_NAME, null, values);
                if (targetBean.id == -1) {
                    return null;
                }
                return targetBean;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public boolean deleteTargetBean(List<TargetBean> list) {
        synchronized (mDataHelper) {
            try {
                String[] arrays = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    arrays[i] = "delete from " + TargetTable.TABLE_NAME + " where " + TargetTable.ID + "=" + list.get(i).id;
                }
                return mDataHelper.execSQL(arrays);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean updateTargetBean(TargetBean targetBean) {
        synchronized (mDataHelper) {
            try {
                ContentValues values = new ContentValues();
                targetBean.writeObject(values, TargetTable.TABLE_NAME);
                return mDataHelper.execSQL(new DatabaseUtils.UpdateParams(values, TargetTable.ID + "=" + targetBean.id, TargetTable.TABLE_NAME));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 目标时间存储操作
     */
    public List<TargetDayBean> queryTargetDayBean() {
        synchronized (mDataHelper) {
            Cursor cursor = null;
            try {
                List<TargetDayBean> list = new ArrayList<>();
                cursor = mDataHelper.rawQuery("select * from " + TargetDayTable.TABLE_NAME + " order by " + TargetDayTable.CREATETIME, null);
                while (cursor.moveToNext()) {
                    TargetDayBean targetDayBean = new TargetDayBean();
                    targetDayBean.readObject(cursor, TargetDayTable.TABLE_NAME);
                    list.add(targetDayBean);
                }
                return list.isEmpty() ? null : list;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        }
        return null;
    }

    public List<TargetDayBean> queryTargetDayBeanByTargetId(long targetId) {
        synchronized (mDataHelper) {
            Cursor cursor = null;
            try {
                List<TargetDayBean> list = new ArrayList<>();
                cursor = mDataHelper.rawQuery("select * from " + TargetDayTable.TABLE_NAME + " where " + TargetDayTable.TARGET_ID + " = " + targetId + " order by " + TargetDayTable.CREATETIME, null);
                while (cursor.moveToNext()) {
                    TargetDayBean targetDayBean = new TargetDayBean();
                    targetDayBean.readObject(cursor, TargetDayTable.TABLE_NAME);
                    list.add(targetDayBean);
                }
                return list.isEmpty() ? null : list;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        }
        return null;
    }

    public TargetDayBean insertTargetDayBean(long targetId, String time) {
        synchronized (mDataHelper) {
            try {
                TargetDayBean targetDayBean = new TargetDayBean(targetId, time);
                ContentValues values = new ContentValues();
                targetDayBean.writeObject(values, TargetDayTable.TABLE_NAME);
                targetDayBean.id = mDataHelper.getWritableDatabase().insert(TargetDayTable.TABLE_NAME, null, values);
                if (targetDayBean.id == -1) {
                    return null;
                }
                return targetDayBean;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public boolean deleteTargetDayBean(List<TargetDayBean> list) {
        synchronized (mDataHelper) {
            try {
                String[] arrays = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    arrays[i] = "delete from " + TargetDayTable.TABLE_NAME + " where " + TargetDayTable.ID + "=" + list.get(i).id;
                }
                return mDataHelper.execSQL(arrays);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean updateTargetDayBean(TargetDayBean targetDayBean) {
        synchronized (mDataHelper) {
            try {
                ContentValues values = new ContentValues();
                targetDayBean.writeObject(values, TargetDayTable.TABLE_NAME);
                return mDataHelper.execSQL(new DatabaseUtils.UpdateParams(values, TargetDayTable.ID + "=" + targetDayBean.id, TargetDayTable.TABLE_NAME));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
